package com.zrrd.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.reggie.bean.ShoppingCart;
import com.zrrd.reggie.mapper.ShoppingCartMapper;
import com.zrrd.reggie.service.ShoppingCartService;
import com.zrrd.reggie.util.BaseContext;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Override
    public ShoppingCart addCart(ShoppingCart cart) {
        Long currentId= BaseContext.getCurrentId();
        cart.setUserId(currentId);
        Long dishId=cart.getDishId();
        QueryWrapper<ShoppingCart> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",currentId);
        if(dishId!=null){
            wrapper.eq("dish_id",dishId);
        }else{
            wrapper.eq("setmeal_id",cart.getSetmealId());
        }
        ShoppingCart carData = this.getOne(wrapper);
        if(carData!=null){
            carData.setNumber(carData.getNumber()+1);
            this.updateById(carData);
        }else {
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            this.save(cart);
            carData=cart;
        }
        return carData;
    }
}
